package com.yuke.cloud.service.cmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.cmc.dto.CmsRedReceiveDto;
import com.yuke.cloud.service.cmc.entity.CmsRedReceive;
import com.yuke.cloud.service.cmc.service.CmsRedReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by zn on 2020/09/19.
*/
@Api(value = "WEB - CmsRedReceiveController", tags = "处理CmsRedReceive相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/red_receive")
public class CmsRedReceiveController extends BaseController {
    @Resource
    private CmsRedReceiveService cmsRedReceiveService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private SmcFeignApi smcFeignApi;


    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsRedReceive", value = "该数据记录的POJO") @RequestBody CmsRedReceive cmsRedReceive) {
        cmsRedReceive.setRedReceiveId(generateId());
        cmsRedReceiveService.save(cmsRedReceive);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsRedReceiveService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsRedReceive", value = "需要修改的内容") @RequestBody CmsRedReceive cmsRedReceive) {
        cmsRedReceiveService.update(cmsRedReceive);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsRedReceive> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsRedReceive cmsRedReceive = cmsRedReceiveService.findById(id);
        return WrapMapper.ok(cmsRedReceive);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsRedReceiveDto>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<CmsRedReceive> list = cmsRedReceiveService.findAll();
        PageInfo pageInfo = new PageInfo(list);

        List<Long> userIdList = list.stream().map(CmsRedReceive::getUserId).collect(Collectors.toList());
        Wrapper<List<UserInfoDto>> userInfos   = uacUserFeignApi.getUserInfoByUserIds(userIdList);

        List<ShopInfoDto> shopInfoDtoList = smcFeignApi.getShopInfoDtoByUserIdList(userIdList);

        List<UserInfoDto> userInfoDtoList = userInfos.getResult();
        List<CmsRedReceiveDto> cmsRedReceiveDtoList = new ArrayList<>();
        for (CmsRedReceive cmsRedReceive : list){

            CmsRedReceiveDto cmsRedReceiveDto = new CmsRedReceiveDto();
            BeanUtils.copyProperties(cmsRedReceive,cmsRedReceiveDto);

            if (userInfoDtoList != null && userInfoDtoList.size() > 0){
                for (UserInfoDto userInfoDto : userInfoDtoList){
                    if (cmsRedReceive.getUserId().equals(userInfoDto.getUserId())){
                        cmsRedReceiveDto.setUserName(userInfoDto.getUserName());
                    }
                }
            }


            if (shopInfoDtoList != null && shopInfoDtoList.size() > 0){
                for (ShopInfoDto shopInfoDto : shopInfoDtoList){
                    if (cmsRedReceive.getUserId().equals(shopInfoDto.getUserId())){
                        cmsRedReceiveDto.setShopName(shopInfoDto.getShopName());
                    }
                }
            }

            cmsRedReceiveDtoList.add(cmsRedReceiveDto);
        }

        pageInfo.setList(cmsRedReceiveDtoList);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/checkRedReceive")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper checkRedReceive(@ApiParam(name = "userId", value = "记录id") @RequestParam(defaultValue = "0") Long userId) {
        if (userId == null || userId == 0){
            WrapMapper.error("参数错误！");
        }
        Integer num = cmsRedReceiveService.checkRedReceive(userId);

        if (num > 0){
            return WrapMapper.error("今天已经签到领取了红包！");
        }
        return WrapMapper.ok(num);
    }

    @PostMapping("/addRedCoupon")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper addRedCoupon(@ApiParam(name = "cmsRedReceive", value = "该数据记录的POJO") @RequestBody CmsRedReceive cmsRedReceive) {
        if (cmsRedReceive == null || cmsRedReceive.getUserId() == null){
            WrapMapper.error("参数错误！");
        }
        Integer num = cmsRedReceiveService.checkRedReceive(cmsRedReceive.getUserId());
        if (num > 0){
            return WrapMapper.error("今天已经签到领取了红包！");
        }

        Integer typeMoney = cmsRedReceiveService.saveRedReceive(cmsRedReceive);
        if (typeMoney.intValue() == 0){
            return WrapMapper.error("领取红包失败！");
        }
        return WrapMapper.ok(typeMoney);
    }

    @GetMapping("/getRedReceive")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper getRedReceive(@ApiParam(name = "userId", value = "记录id") @RequestParam(defaultValue = "0") Long userId) {
        if (userId == null || userId == 0){
            WrapMapper.error("参数错误！");
        }
        List<CmsRedReceiveDto> cmsRedReceives = cmsRedReceiveService.getRedReceive(userId);

        return WrapMapper.ok(cmsRedReceives);
    }

    @PostMapping("/selectRedReceive")
    @ApiOperation(httpMethod = "POST", value = "分页条件获取记录")
    public Wrapper<PageInfo<CmsRedReceiveDto>> selectRedReceiveByCondition(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                           @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                           @ApiParam(name = "cmsRedReceive", value = "该数据记录的POJO") @RequestBody CmsRedReceiveDto cmsRedReceiveDto) {

        if (cmsRedReceiveDto != null && cmsRedReceiveDto.getUserId() != null ){
            PageHelper.startPage(page, size);
            List<CmsRedReceiveDto> cmsRedReceiveDtoList = cmsRedReceiveService.selectRedReceiveByCondition(cmsRedReceiveDto);
            PageInfo pageInfo = new PageInfo(cmsRedReceiveDtoList);
            return WrapMapper.ok(pageInfo);
        }

        return WrapMapper.ok();
    }



}
